package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.feign.ApArticleFeignClient;
import com.heima.feign.WmUserFeignClient;
import com.heima.model.article.dtos.AuthorDto;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.dtos.AuthFailDto;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserAuth;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.service.ApUserAuthService;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserAuth> implements ApUserAuthService {

    @Autowired
    private ApUserService userService;

    @Autowired
    private WmUserFeignClient wmUserFeignClient;

    @Autowired
    private ApArticleFeignClient articleFeignClient;

    @Override
    public void authFail(AuthFailDto dto) {
        // 更新认证状态为失败
        updateUserAuthStatus(dto.getId(), dto.getMsg(), 2);//TODO 枚举
    }

    // @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public void authPass(AuthFailDto dto) {
        ApUserAuth userAuth = this.getById(dto.getId());
        if(userAuth == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Integer userId = userAuth.getUserId();
        ApUser userDB = userService.getById(userId);
        if(userDB == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 更新认证状态为通过
        updateUserAuthStatus(dto.getId(), "", 9);//TODO 枚举
        // 更新用户的身份 自媒体人
        userService.updateToWemdiaUser(userAuth.getUserId());
        // 生成自媒体人
        Integer wmUserId = wmUserFeignClient.createUser(buildWmUserDto(userDB));
        // 生成文章的作者
        articleFeignClient.createAuthor(buildAuthorDTO(wmUserId, userDB));
        try{
            Thread.sleep(3000);
        }catch(Exception e){e.printStackTrace();}
        System.out.println(1/0);
    }

    private void updateUserAuthStatus(Integer id, String reason, Integer status){
        ApUserAuth toBeUpdate = new ApUserAuth();
        toBeUpdate.setId(id);
        toBeUpdate.setReason(reason);
        toBeUpdate.setStatus(status);//TODO 枚举
        this.updateById(toBeUpdate);
    }

    private AuthorDto buildAuthorDTO(Integer wmUserId, ApUser userDB) {
        AuthorDto dto = new AuthorDto();
        dto.setName(userDB.getName());
        dto.setUserId(userDB.getId());
        dto.setWmUserId(wmUserId);
        dto.setType(2);//TODO
        return dto;
    }

    private WmUserDto buildWmUserDto(ApUser userDB) {
        WmUserDto wmUserDto = new WmUserDto();
        wmUserDto.setApUserId(userDB.getId());
        wmUserDto.setName(userDB.getName());
        wmUserDto.setPassword(userDB.getPassword());
        wmUserDto.setPhone(userDB.getPhone());
        wmUserDto.setStatus(9);
        return wmUserDto;
    }

    @Override
    public PageResult<ApUserAuth> findByPage(AuthDto dto) {
        LambdaQueryWrapper<ApUserAuth> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStatus()!=null, ApUserAuth::getStatus, dto.getStatus());
        // 分页参数
        Page<ApUserAuth> ipage = new Page<>(dto.getPage(), dto.getSize());
        // 查询
        Page<ApUserAuth> page = this.page(ipage, queryWrapper);
        if(CollectionUtils.isEmpty(page.getRecords())){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return new PageResult<> (dto, page.getTotal(), page.getRecords());
    }
}
